import java.util.Comparator;

public class SelectionSort<T> {
    private Comparator<T> comparator;

    public SelectionSort(Comparator<T> comparator) {
        this.comparator = comparator;
    }

    public void sort(T[] array) {
        for (int end = array.length - 1; end > 0; --end) {
            int targetIndex = 0;
            for (int i = 0; i <= end; ++i) {
                if (comparator.compare(array[i], array[targetIndex]) > 0) {
                    targetIndex = i;
                }
            }

            swap(array, end, targetIndex);
        }
    }

    private static <T> void swap(T[] array, int i, int j) {
        T temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }


}
